// pages/center/register/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: '',
      nickName: '',
      mobile: ''
    },
    // 是否显示获取手机号按钮
    mobileBtnShow: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userInfo: wx.getStorageSync('wxUserInfo')
    })
  },

  // 名称输入监听
  bindNameInput(e) {
    this.setData({
      ['userInfo.nickName']: e.detail.value
    })
  },

  // 手机号码输入监听
  bindMobileInput(e) {
    console.log(e);
    this.setData({
      ['userInfo.mobile']: e.detail.value
    });
    if (this.data.userInfo.mobile === '') {
      this.setData({
        mobileBtnShow: true
      })
    }
  },

  // 获取手机号码
  getPhoneNumber(e) {
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      var that = this;

      let wxType = wx.getSystemInfoSync().environment ? 0 : 1;

      if (wxType) {
        wx.login({
          success: function(res) {
            var data = {
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv,
              code: res.code,
              point_url: app.server.agentApi.getPhoneNumber,
              wxType: wxType
            };
            app.server.getConnect(data).then(function(res) {
              that.setData({
                ['userInfo.mobile']: res.data['phoneNumber'],
                mobileBtnShow: false
              });
              that.bindRegister();
            })
          }
        })
      } else {
        wx.qy.login({
          success: function (res) {
            var data = {
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv,
              code: res.code,
              point_url: app.server.agentApi.getPhoneNumber,
              wxType: wxType
            };
            app.server.getConnect(data).then(function (res) {
              that.setData({
                ['userInfo.mobile']: res.data['phoneNumber'],
                mobileBtnShow: false
              });
              that.bindRegister();
            })
          }
        })
      }
    }
  },

  // 执行注册
  bindRegister() {
    var that = this;
    var data = this.data.userInfo;
    data['point_url'] = app.server.agentApi.register;
    app.server.getConnect(data).then(function(res) {
      that.getUserInfo();
    })
  },

  getUserInfo: function() {
    var that = this;
    var data = {
      point_url: app.server.agentApi.getMemberDetail
    };
    app.server.getConnect(data).then(function(res) {
      wx.setStorageSync("userInfo", res.data);
      app.globalData.userInfo = res.data
      wx.navigateBack();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})